from app import db
from datetime import datetime

class Equipment(db.Model):
    __tablename__ = 'equipment'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False)
    model = db.Column(db.String(64), nullable=False)
    type = db.Column(db.String(32), nullable=False)  # sensor, controller, actuator
    manufacturer = db.Column(db.String(128))
    specifications = db.Column(db.JSON)  # 技术规格
    price = db.Column(db.Float)  # 单价
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def __init__(self, name, model, type, manufacturer=None, specifications=None, price=None):
        self.name = name
        self.model = model
        self.type = type
        self.manufacturer = manufacturer
        self.specifications = specifications or {}
        self.price = price

class ProjectEquipment(db.Model):
    __tablename__ = 'project_equipment'
    
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), nullable=False)
    equipment_id = db.Column(db.Integer, db.ForeignKey('equipment.id'), nullable=False)
    quantity = db.Column(db.Integer, default=1)
    location = db.Column(db.String(128))  # 安装位置
    notes = db.Column(db.Text)  # 备注
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    equipment = db.relationship('Equipment', backref='project_equipment')
    project = db.relationship('Project', backref='project_equipment')

class Sensor(db.Model):
    __tablename__ = 'sensors'
    
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
    name = db.Column(db.String(128))
    type = db.Column(db.String(64))  # 热电偶、压力变送器等
    specifications = db.Column(db.JSON)  # 精度、量程、响应速度等
    cost = db.Column(db.Float)
    selection_reason = db.Column(db.Text)

class Controller(db.Model):
    __tablename__ = 'controllers'
    
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
    name = db.Column(db.String(128))
    type = db.Column(db.String(64))  # PLC、DCS等
    specifications = db.Column(db.JSON)  # 功能、可扩展性、兼容性等
    cost = db.Column(db.Float)
    selection_reason = db.Column(db.Text)

class DataAcquisition(db.Model):
    __tablename__ = 'data_acquisitions'
    
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
    name = db.Column(db.String(128))
    type = db.Column(db.String(64))  # 数据采集卡、通信模块等
    specifications = db.Column(db.JSON)  # 传输速率、距离、协议等
    cost = db.Column(db.Float)
    selection_reason = db.Column(db.Text) 